//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package net.mingsoft.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.spring.stat.BeanTypeAutoProxyCreator;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import net.mingsoft.base.MSVersion;
import net.mingsoft.basic.filter.XSSEscapeFilter;
import net.mingsoft.basic.interceptor.ActionInterceptor;
import net.mingsoft.referer.RefererInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.context.request.RequestContextListener;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author gengfa
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Value("${ms.upload.path}")
    private String uploadFloderPath;
    @Value("${ms.upload.template}")
    private String template;
    @Value("${ms.upload.mapping}")
    private String uploadMapping;
    @Value("${ms.diy.html-dir:html}")
    private String htmlDir;

    WebConfig() {
        System.out.println("___  ____             _____        __ _    ___  ___ _____ ");
        System.out.println("|  \\/  (_)           /  ___|      / _| |   |  \\/  |/  ___|");
        System.out.println("| .  . |_ _ __   __ _\\ `--.  ___ | |_| |_  | .  . |\\ `--. ");
        System.out.println("| |\\/| | | '_ \\ / _` |`--. \\/ _ \\|  _| __| | |\\/| | `--. \\");
        System.out.println("| |  | | | | | | (_| /\\__/ / (_) | | | |_  | |  | |/\\__/ /");
        System.out.println("\\_|  |_/_|_| |_|\\__, \\____/ \\___/|_|  \\__| \\_|  |_/\\____/ ");
        System.out.println("                 __/ |                                    ");
        System.out.println("                |___/                                     ");
        System.out.println(" :: MingSoft Frame  Library ::         (v" + MSVersion.getVersion() + ")");
    }

    @Bean
    public ActionInterceptor actionInterceptor() {
        return new ActionInterceptor();
    }

    @Bean
    public RefererInterceptor refererInterceptor() {
        return new RefererInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(this.actionInterceptor()).excludePathPatterns("/static/**", "/app/**", "/webjars/**", "/*.html", "/*.htm");
        registry.addInterceptor(new net.mingsoft.people.interceptor.ActionInterceptor("/mdiyPage/login.do",""))
                .addPathPatterns("/people/**");
        registry.addInterceptor(refererInterceptor()).addPathPatterns("/**");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler(this.uploadMapping).addResourceLocations(File.separator + this.uploadFloderPath + File.separator, "file:" + this.uploadFloderPath + File.separator);
        registry.addResourceHandler("/template/**").addResourceLocations(File.separator + this.template + File.separator, "file:" + this.template + File.separator);
        registry.addResourceHandler("/" + this.htmlDir + "/**").addResourceLocations("/" + this.htmlDir + "/", "file:" + this.htmlDir + "/");
        registry.addResourceHandler("/app/**").addResourceLocations("/app/", "file:app/", "classpath:/app/");
        registry.addResourceHandler("/static/**").addResourceLocations("/static/", "file:static/", "classpath:/static/", "classpath:/META-INF/resources/");
        registry.addResourceHandler("/api/**").addResourceLocations("/api/", "file:api/", "classpath:/api/");
        if ((new File(this.uploadFloderPath)).isAbsolute()) {
            registry.addResourceHandler(this.uploadMapping).addResourceLocations("file:" + this.uploadFloderPath + File.separator);
        }

    }

    @Bean
    public BeanTypeAutoProxyCreator beanTypeAutoProxyCreator() {
        BeanTypeAutoProxyCreator beanTypeAutoProxyCreator = new BeanTypeAutoProxyCreator();
        beanTypeAutoProxyCreator.setTargetBeanType(DruidDataSource.class);
        beanTypeAutoProxyCreator.setInterceptorNames("druidStatInterceptor");
        return beanTypeAutoProxyCreator;
    }

    @Bean
    public FilterRegistrationBean xssFilterRegistration() {
        XSSEscapeFilter xssFilter = new XSSEscapeFilter();
        FilterRegistrationBean registration = new FilterRegistrationBean(xssFilter);
        xssFilter.includes.add(".*/search.do");
        registration.setName("XSSFilter");
        registration.addUrlPatterns(new String[]{"/*"});
        registration.setOrder(-2147483648);
        return registration;
    }

    @Bean
    public ServletListenerRegistrationBean<RequestContextListener> requestContextListenerRegistration() {
        return new ServletListenerRegistrationBean(new RequestContextListener());
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("forward:/index");
        registry.setOrder(-2147483648);
        WebMvcConfigurer.super.addViewControllers(registry);
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(this.mappingJackson2HttpMessageConverter());
        WebMvcConfigurer.super.configureMessageConverters(converters);
    }

    @Bean
    public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        converter.setObjectMapper(objectMapper);
        return converter;
    }

    @Bean
    public ExecutorService crawlExecutorPool() {
        ExecutorService pool = new ThreadPoolExecutor(20, 20, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue());
        return pool;
    }
}
